package com.smfx.yxt.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class StringUtil {
	
	@SuppressWarnings("rawtypes")
	public static boolean isNullOrEmpty(Collection c){
        return c==null||c.isEmpty();
    }

	public static String getCourseOrderNum(Integer userId) {
		Date date = new Date();
		String dateTime = date.getTime()+"";
		String str = 1 + dateTime.substring(dateTime.length()-6, dateTime.length()) + userId;
		return str;
	}
	
	public static String emptyToNull(String s) {
    	if(s != null && "".equals(s)) return null;
    	return s;
    }
	
	public static String nullToEmpty(String s) {
    	if(s == null) return "";
    	return s;
    }
	
	public static boolean isNotNullOrZero(Integer v) {
		if(v != null && v.intValue() > 0) {
			return true;
		}
		return false;
	}
	
	public static Integer stringToInt(String str) {
		try {
			if(StringUtils.isBlank(str)) return null;
			return Integer.valueOf(str);
		} catch (Exception e) {}
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String delRepeatStr(String str) {
		String[] s = str.split(",");
		List<String> list = Arrays.asList(s);
		Set set = new HashSet(list);
		String [] rid=(String [])set.toArray(new String[0]);
		return String.join(",", rid);
	}
	
	public static boolean isExistPosition(String array, String key) {
		if(StringUtils.isNotBlank(array) && StringUtils.isNotBlank(key)) {
			String[] keys = key.split(",");
			for(String v : keys) {
				if(("," + array + ",").indexOf("," + v + ",") != -1) {
					return true;
				}
			}
		}
		return false;
	}
	
	// 删除ArrayList中重复元素，保持顺序     
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List removeDuplicate(List list) {   
		HashSet h = new HashSet(list);   
		list.clear();   
		list.addAll(h);   
		return list;   
	}   
	
	public static String removeSpecialChar(String v) {
		String regEx="[\n`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。， 、？]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(v);//这里把想要替换的字符串传进来
		return m.replaceAll("").trim();
	}
}
